package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.fileOperation.FileMakeDTO;
import com.etone.smartAudit.dto.fileOperation.FileMoveOrCopyDTO;
import com.etone.smartAudit.dto.fileOperation.FileRenameDTO;
import com.etone.smartAudit.dto.fileOperation.compress.CompressDTO;
import com.etone.smartAudit.dto.fileOperation.compress.DecompressDTO;
import com.etone.smartAudit.service.ServerFileOperationService;
import com.etone.smartAudit.vo.fileOperation.FileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author: sovy
 * @date: 2020/11/24
 * @description:
 */
@AllArgsConstructor
@RestController
@Api(tags = "服务器文件操作", value = "ServerCollectController")
public class ServerFileOperationController {

    private ServerFileOperationService serverFileOperationService;

    @ApiOperation(value = "文件列表《完》")
    @GetMapping("/serverFiles/list")
    public List<FileVO> fileList(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目id") @RequestParam Integer itemId,
            @ApiParam(value = "服务器id") @RequestParam Integer serverId,
            @ApiParam(value = "目录") @RequestParam String path
    ) {
        return serverFileOperationService.fileList(admin, itemId, serverId, path);
    }

    @ApiOperation(value = "文件重命名《完》")
    @PutMapping("/serverFiles/rename")
    public void rename(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目id") @RequestParam Integer itemId,
            @ApiParam(value = "服务器id") @RequestParam Integer serverId,
            @ApiParam(value = "文件重命名信息") @RequestBody @Validated FileRenameDTO fileRenameDTO
    ) {
        serverFileOperationService.rename(admin, itemId, serverId, fileRenameDTO);
    }

    @ApiOperation(value = "文件移动《完》")
    @PutMapping("/serverFiles/move")
    public void move(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目id") @RequestParam Integer itemId,
            @ApiParam(value = "服务器id") @RequestParam Integer serverId,
            @ApiParam(value = "文件移动信息") @RequestBody @Validated List<FileMoveOrCopyDTO> fileMoveOrCopyDTOS
    ) {
        serverFileOperationService.move(admin, itemId, serverId, fileMoveOrCopyDTOS);
    }

    @ApiOperation(value = "文件复制《完》")
    @PutMapping("/serverFiles/copy")
    public void copy(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目id") @RequestParam Integer itemId,
            @ApiParam(value = "服务器id") @RequestParam Integer serverId,
            @ApiParam(value = "文件复制信息") @RequestBody @Validated List<FileMoveOrCopyDTO> fileMoveOrCopyDTOS
    ) {
        serverFileOperationService.copy(admin, itemId, serverId, fileMoveOrCopyDTOS);
    }

    @ApiOperation(value = "文件删除《完》")
    @DeleteMapping("/serverFiles/delete")
    public void delete(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目id") @RequestParam Integer itemId,
            @ApiParam(value = "服务器id") @RequestParam Integer serverId,
            @ApiParam(value = "文件或者目录") @RequestParam List<String> files
    ) {
        serverFileOperationService.delete(admin, itemId, serverId, files);
    }

    @ApiOperation(value = "新建文件夹《完》")
    @PostMapping("/serverFiles/mkdir")
    public void mkdir(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目id") @RequestParam Integer itemId,
            @ApiParam(value = "服务器id") @RequestParam Integer serverId,
            @ApiParam(value = "新建文件") @RequestBody @Validated FileMakeDTO fileMakeDTO
    ) {
        serverFileOperationService.mkdir(admin, itemId, serverId, fileMakeDTO);
    }

    @ApiOperation(value = "压缩文件《完》")
    @PostMapping("/serverFiles/compress")
    private void compress(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目id") @RequestParam Integer itemId,
            @ApiParam(value = "服务器id") @RequestParam Integer serverId,
            @ApiParam(value = "压缩信息") @RequestBody @Validated CompressDTO compressDTO
    ) {
        serverFileOperationService.compress(admin, itemId, serverId, compressDTO);
    }

    @ApiOperation(value = "解压文件《完》")
    @PostMapping("/serverFiles/decompress")
    private void decompress(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目id") @RequestParam Integer itemId,
            @ApiParam(value = "服务器id") @RequestParam Integer serverId,
            @ApiParam(value = "解压信息") @RequestBody @Validated DecompressDTO decompressDTO
    ) {
        serverFileOperationService.decompress(admin, itemId, serverId, decompressDTO);
    }
}
